import Vue from 'vue'
import router from '@/router'
import store from '@/store'

/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

/**
 * 是否有权限
 * @param {*} key
 */
export function isAuth (key) {
  return JSON.parse(sessionStorage.getItem('permissions') || '[]').indexOf(key) !== -1 || false
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate (data, id = 'id', pid = 'parentId', display = true) {
  let res = []
  let temp = {}
  for (var i = 0; i < data.length; i++) {
    if (data[i].display === 1 && display) {

    }
    temp[data[i][id]] = data[i]
  }
  for (let k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]]['children']) {
        temp[data[k][pid]]['children'] = []
      }
      if (!temp[data[k][pid]]['_level']) {
        temp[data[k][pid]]['_level'] = 1
      }
      data[k]['_level'] = temp[data[k][pid]]._level + 1
      temp[data[k][pid]]['children'].push(data[k])
    } else {
      res.push(data[k])
    }
  }
  return res
}

/**
 * 闭包模式
 * @param data
 * @param id
 * @param pid
 * @param display
 */
export function treeDataTranslateChange (data, id = 'id', pid = 'parentId', display = true) {
  let secendTree = (d,idd) => {
    let child = [];
    for (let i in d) {
      if (d[i][pid] === idd) {
        child.push(d[i])
      }
    }
    return child
  }
  let firstTree = (d,idd) => {
    let tree = secendTree(d,idd);
    if (tree.length === 0) {
      return [];
    }
    for (let i in tree) {
      let child = firstTree(d, tree[i][id])
      if (child.length > 0) {
        tree[i]['children'] = child
      }
    }
    return tree;
  }
  let dat = firstTree(data,0) || null
  return dat;
}

/**
 * 清除登录信息
 */
export function clearLoginInfo () {

  Vue.cookie.delete('admin')
  store.commit('resetStore')
  router.options.isAddDynamicMenuRoutes = false
}

/**
 * 加载外部js
 * @param src
 * @returns {Promise<any>}
 */
export function loadJs (src) {

  return new Promise((resolve, reject) => {

    let script = document.createElement('script')
    script.type = 'text/javascript'
    script.src = src
    document.body.appendChild(script)
    script.onload = () => {
      resolve()
    }
    script.onerror = () => {
      reject()
    }

  })

}

export function loadCss (src) {
  let style = document.createElement('link')
  style.setAttribute('rel', 'stylesheet')
  style.setAttribute('href', src)
  document.body.appendChild(style)

}

/**
 * 时间戳转换时间
 * @param time
 */
export function changeTime (time, type) {
  let str = parseInt(time) * 1000
  let newDate = new Date(str)
  let year = newDate.getUTCFullYear()
  let month = newDate.getUTCMonth()
  let nowday = newDate.getUTCDate()
  let hours = newDate.getHours() //取小时
  let minutes = newDate.getMinutes() //取分钟
  let seconds = newDate.getSeconds() //取秒
  hours = (hours < 10) ? '0' + hours : hours
  minutes = (minutes < 10) ? '0' + minutes : minutes
  let typeArr = {
    0: year + '-' + month + '-' + nowday + '' + ' ' + hours + ':' + minutes,
    1: year + '.' + month + '.' + nowday,
  }

  return typeArr[type]
}
